home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / gfx / misc / gnuplot-src.lha / gnuplot-3.7.1src / gnuplot-3.7.1.lha / gnuplot-3.7.1 / docs / doc2ipf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-19  |  11.3 KB  |  464 lines

  1. #ifndef lint]=='1'?line[0]:lSid = "$Id: doc2ipf.c,v 1.7 1998/10/19 13:17:45 lhecking Exp $";
  2. #endif
  3.  
  4. /* GNUPLOT - doc2ipf.c */
  5.  
  6. /*[
  7.  * Copyright 1986 - 1993, 1998   Thomas Williams, Colin Kelley
  8.  *
  9.  * Permission to use, copy, and distribute this software and its
  10.  * documentation for any purpose with or without fee is hereby granted,
  11.  * provided that the above copyright notice appear in all copies and
  12.  * that both that copyright notice and this permission notice appear
  13.  * in supporting documentation.
  14.  *
  15.  * Permission to modify the software is granted, but not the right to
  16.  * distribute the complete modified source code.  Modifications are to
  17.  * be distributed as patches to the released version.  Permission to
  18.  * distribute binaries produced by compiling modified sources is granted,
  19.  * provided you
  20.  *   1. distribute the corresponding source modifications from the
  21.  *    released version in the form of a patch file along with the binaries,
  22.  *   2. add special version identification to distinguish your version
  23.  *    in addition to the base release version number,
  24.  *   3. provide your name and address as the primary contact for the
  25.  *    support of your modified version, and
  26.  *   4. retain our contact information in regard to use of the base
  27.  *    software.
  28.  * Permission to distribute the released version of the source code along
  29.  * with corresponding source modifications in the form of a patch file is
  30.  * granted with same provisions 2 through 4 for binary distributions.
  31.  *
  32.  * This software is provided "as is" without express or implied warranty
  33.  * to the extent permitted by applicable law.
  34. ]*/
  35.  
  36. /*
  37.  * doc2ipf.c  -- program to convert Gnuplot .DOC format to OS/2
  38.  * ipfc  (.inf/.hlp) format.
  39.  *
  40.  * Modified by Roger Fearick from doc2rtf by M Castro 
  41.  *
  42.  * usage:  doc2ipf gnuplot.doc gnuplot.itl
  43.  *
  44.  */
  45.  
  46. /* note that tables must begin in at least the second column to */
  47. /* be formatted correctly and tabs are forbidden */
  48.  
  49. #ifdef HAVE_CONFIG_H
  50. # include "config.h"
  51. #endif
  52.  
  53. #include "ansichek.h"
  54. #include "stdfn.h"
  55.  
  56. #define MAX_LINE_LEN 1023
  57.  
  58. #include "doc2x.h"
  59. #include "xref.h"
  60.  
  61. #define MAX_COL 6
  62.  
  63. /* From xref.c */
  64. extern void *xmalloc __PROTO((size_t));
  65.  
  66. void convert __PROTO((FILE *, FILE *));
  67. void process_line __PROTO((char *, FILE *));
  68.  
  69. /* malloc's are not being checked ! */
  70.  
  71. struct TABENTRY {        /* may have MAX_COL column tables */
  72.     struct TABENTRY *next;
  73.     char col[MAX_COL][256];
  74. };
  75.  
  76. struct TABENTRY table = { NULL };
  77. struct TABENTRY *tableins = &table;
  78. int tablecols = 0;
  79. int tablewidth[MAX_COL] = {0, 0, 0, 0, 0, 0};    /* there must be the correct */
  80. int tablelines = 0;        /* number of zeroes here */
  81.  
  82. static boolean debug = FALSE;
  83.  
  84.  
  85. int main(argc, argv)
  86. int argc;
  87. char **argv;
  88. {
  89.     FILE *infile;
  90.     FILE *outfile;
  91.     if (argc == 4 && argv[3][0] == '-' && argv[3][1] == 'd')
  92.     debug = TRUE;
  93.  
  94.     if (argc != 3 && !debug) {
  95.     fprintf(stderr, "Usage: %s infile outfile\n", argv[0]);
  96.     exit(EXIT_FAILURE);
  97.     }
  98.     if ((infile = fopen(argv[1], "r")) == (FILE *) NULL) {
  99.     fprintf(stderr, "%s: Can't open %s for reading\n",
  100.         argv[0], argv[1]);
  101.     exit(EXIT_FAILURE);
  102.     }
  103.     if ((outfile = fopen(argv[2], "w")) == (FILE *) NULL) {
  104.     fprintf(stderr, "%s: Can't open %s for writing\n",
  105.         argv[0], argv[2]);
  106.     fclose(infile);
  107.     exit(EXIT_FAILURE);
  108.     }
  109.     parse(infile);
  110.     convert(infile, outfile);
  111.     exit(EXIT_SUCCESS);
  112. }
  113.  
  114. void convert(a, b)
  115. FILE *a, *b;
  116. {
  117.     static char line[MAX_LINE_LEN+1];
  118.  
  119.     /* generate ipf header */
  120.     fprintf(b, ":userdoc.\n:prolog.\n");
  121.     fprintf(b, ":title.GNUPLOT\n");
  122.     fprintf(b, ":docprof toc=12345.\n:eprolog.\n");
  123.  
  124.     /* process each line of the file */
  125.     while (get_line(line, sizeof(line), a)) {
  126.     process_line(line, b);
  127.     }
  128.  
  129.     /* close final page and generate trailer */
  130.     fprintf(b, "\n:euserdoc.\n");
  131.  
  132.     list_free();
  133. }
  134.  
  135. void process_line(line, b)
  136. char *line;
  137. FILE *b;
  138. {
  139.     static int line_count = 0;
  140.     static char line2[MAX_LINE_LEN+1];
  141.     static int last_line;
  142.     char hyplink1[64];
  143.     char *pt, *tablerow;
  144.     int i;
  145.     int j;
  146.     static int startpage = 1;
  147.     char str[MAX_LINE_LEN+1];
  148.     char topic[MAX_LINE_LEN+1];
  149.     int k, l;
  150.     static int tabl = 0;
  151.     static int para = 0;
  152.     static int inquote = FALSE;
  153.     static int inref = FALSE;
  154.     static int intable = FALSE;
  155.     static int intablebut = FALSE;
  156.     static int introffheader = FALSE;
  157.     static char tablechar = '@';
  158.     static FILE *bo = NULL, *bt = NULL;
  159.     static char tabledelim[4] = "%@\n";
  160.     static int nblanks = 0;
  161.     struct LIST *klist;
  162.  
  163.     line_count++;
  164.  
  165.     if (introffheader)
  166.     fprintf(stderr, "%s\n", line);
  167.     if (bo == NULL)
  168.     bo = b;
  169.     i = 0;
  170.     j = 0;
  171.     nblanks = 0;
  172.     while (line[nblanks] == ' ')
  173.     ++nblanks;
  174.     while (line[i] != NUL) {
  175.     if (introffheader) {
  176.         if (line[i] != '\n')
  177.         line2[j] = line[i];
  178.         else
  179.         line2[j] = NUL;
  180.     } else
  181.         switch (line[i]) {
  182.         case '$':
  183.         if (intable && (tablechar != '$') && (line[0] == '%')) {
  184.             ++i;
  185.             if (line[i + 1] == '$' || line[i] == 'x' || line[i] == '|') {
  186.             while (line[i] != '$')
  187.                 line2[j++] = line[i++];
  188.             --j;
  189.             } else {
  190.             while (line[i] != '$')
  191.                 i++;
  192.             if (line[i + 1] == ',')
  193.                 i++;
  194.             if (line[i + 1] == ' ')
  195.                 i++;
  196.             line2[j] = line[++i];
  197.             }
  198.         } else
  199.             line2[j] = line[i];
  200.         break;
  201.         case ':':
  202.         strcpy(&line2[j], "&colon.");
  203.         j += strlen("&colon.") - 1;
  204.         break;
  205.  
  206.         case '&':
  207.         /* real hack to solve \&_ in postscript doc tables */
  208.         /* (which are a special case hack anyway. */
  209.         if (j > 0 && line2[j - 1] == '\\') {
  210.             j -= 2;
  211.             break;
  212.         }
  213.         strcpy(&line2[j], "&.");
  214.         j += strlen("&.") - 1;
  215.         break;
  216.  
  217.         case '\r':
  218.         case '\n':
  219.         break;
  220.         case '`':        /* backquotes mean boldface or link */
  221.         if (nblanks > 7) {
  222.             line2[j] = line[i];
  223.             break;
  224.         }
  225.         if ((!inref) && (!inquote)) {
  226.             k = i + 1;    /* index into current string */
  227.             l = 0;    /* index into topic string */
  228.             while ((line[k] != '`') && (line[k] != 0)) {
  229.             topic[l] = line[k];
  230.             k++;
  231.             l++;
  232.             }
  233.             topic[l] = 0;
  234.             klist = lookup(topic);
  235.             if (klist != NULL && (k = klist->line) > 0) {
  236.             sprintf(hyplink1, ":link reftype=hd res=%d.", k);
  237.             strcpy(line2 + j, hyplink1);
  238.             j += strlen(hyplink1) - 1;
  239.  
  240.             inref = k;
  241.             } else {
  242.             if (debug)
  243.                 fprintf(stderr, "Can't make link for \042%s\042 on line %d\n", topic, line_count);
  244.             strcpy(line2 + j, ":hp2.");
  245.             j += 4;
  246.             inquote = TRUE;
  247.             }
  248.         } else {
  249.             if (inquote && inref)
  250.             fprintf(stderr, "Warning: Reference Quote conflict line %d\n", line_count);
  251.             if (inquote) {
  252.             strcpy(line2 + j, ":ehp2.");
  253.             j += 5;
  254.             inquote = FALSE;
  255.             }
  256.             if (inref) {
  257.             /* must be inref */
  258.             strcpy(line2 + j, ":elink.");
  259.             j += 6;
  260.             inref = FALSE;
  261.             }
  262.         }
  263.         break;
  264.         default:
  265.         line2[j] = line[i];
  266.         }
  267.     i++;
  268.     j++;
  269.     if ((j >= sizeof(line2))) {
  270.         fprintf(stderr, "MAX_LINE_LEN exceeded\n");
  271.         if (inref || inquote)
  272.         fprintf(stderr, "Possible missing link character (`) near above line number\n");
  273.         abort();
  274.     }
  275.     line2[j] = NUL;
  276.     }
  277.  
  278.     i = 1;
  279.  
  280.     switch (line[0]) {        /* control character */
  281.     case '?':{            /* interactive help entry */
  282.         if (intable)
  283.         intablebut = TRUE;
  284.         break;
  285.     }
  286.     case '@':{            /* start/end table */
  287.         intable = !intable;
  288.         if (intable) {
  289.         tablechar = '@';
  290.         introffheader = FALSE;
  291.         tablelines = 0;
  292.         tablecols = 0;
  293.         tableins = &table;
  294.         for (j = 0; j < MAX_COL; j++)
  295.             tablewidth[j] = 0;
  296.         } else {        /* dump table */
  297.         int header = 0;
  298.         intablebut = FALSE;
  299.         tableins = &table;
  300.         fprintf(b, ":table cols=\'");
  301.         for (j = 0; j < MAX_COL; j++)
  302.             if (tablewidth[j] > 0)
  303.             fprintf(b, " %d", tablewidth[j]);
  304.         fprintf(b, "\'.\n");
  305.         tableins = tableins->next;
  306.         if (tableins->next != NULL)
  307.             header = (tableins->next->col[0][0] == '_');
  308.         if (header)
  309.             tableins->next = tableins->next->next;
  310.         while (tableins != NULL) {
  311.             fprintf(b, ":row.\n");
  312.             for (j = 0; j < tablecols; j++)
  313.             if (header)
  314.                 fprintf(b, ":c.:hp9.%s:ehp9.\n", tableins->col[j]);
  315.             else
  316.                 fprintf(b, ":c.%s\n", tableins->col[j]);
  317.             tableins = tableins->next;
  318.             header = 0;
  319.         }
  320.         fprintf(b, ":etable.\n");
  321.         if (bt != NULL) {
  322.             rewind(bt);
  323.             while (get_line(str, sizeof(str), bt))
  324.             fputs(str, b);
  325.             fclose(bt);
  326.             remove("doc2ipf.tmp");
  327.             bt = NULL;
  328.             bo = b;
  329.         }
  330.         }
  331.         break;
  332.     }
  333.     case '#':{            /* latex table entry */
  334.         break;        /* ignore */
  335.     }
  336.     case '%':{            /* troff table entry */
  337.         if (intable) {
  338.         if (introffheader) {
  339.             fprintf(stderr, ">%s\n", line2);
  340.             fprintf(stderr, "tablechar: %c\n", tablechar);
  341.             if (line2[1] == '.')
  342.             break;
  343.             pt = strchr(line2, '(');
  344.             if (pt != NULL)
  345.             tablechar = *(pt + 1);
  346.             fprintf(stderr, "tablechar: %c\n", tablechar);
  347.             pt = strchr(line2 + 2, '.');
  348.             if (pt != NULL)
  349.             introffheader = FALSE;
  350.             break;
  351.         }
  352.         if (line[1] == '.') {    /* ignore troff commands */
  353.             introffheader = TRUE;
  354.             break;
  355.         }
  356.         tablerow = line2;
  357.         tableins->next = xmalloc(sizeof(struct TABENTRY));
  358.         tableins = tableins->next;
  359.         tableins->next = NULL;
  360.         j = 0;
  361.         tabledelim[1] = tablechar;
  362.         line2[0] = tablechar;
  363.         while ((pt = strtok(tablerow, tabledelim + 1)) != NULL) {
  364.             if (*pt != NUL) {    /* ignore null columns */
  365.             /* this fails on format line */
  366.             assert(j < MAX_COL);
  367.             strcpy(tableins->col[j], pt);
  368.             k = strlen(pt);
  369.             if (k > tablewidth[j])
  370.                 tablewidth[j] = k;
  371.             ++j;
  372.             tablerow = NULL;
  373.             if (j > tablecols)
  374.                 tablecols = j;
  375.             }
  376.         }
  377.         while (j < MAX_COL)
  378.             tableins->col[j++][0] = NUL;
  379.         }
  380.         break;        /* ignore */
  381.     }
  382.     case '\n':            /* empty text line */
  383.     para = 0;
  384.     tabl = 0;
  385.     fprintf(bo, ":p.");
  386.     break;
  387.     case ' ':{            /* normal text line */
  388.         if (intable && !intablebut)
  389.         break;
  390.         if (intablebut) {    /* indexed items in  table, copy
  391.                    to file after table by saving in
  392.                    a temp file meantime */
  393.         if (bt == NULL) {
  394.             fflush(bo);
  395.             bt = fopen("doc2ipf.tmp", "w+");
  396.             if (bt == NULL)
  397.             fprintf(stderr, "cant open temp\n");
  398.             else
  399.             bo = bt;
  400.         }
  401.         }
  402.         if (intablebut && (bt == NULL))
  403.         break;
  404.         if ((line2[1] == 0) || (line2[1] == '\n')) {
  405.         fprintf(bo, ":p.");
  406.         para = 0;
  407.         }
  408.         if (line2[1] == ' ') {
  409.         if (!tabl)
  410.             fprintf(bo, ":p.");
  411.         fprintf(bo, "%s", &line2[1]);
  412.         fprintf(bo, "\n.br\n");
  413.         tabl = 1;
  414.         para = 0;
  415.         } else {
  416.         if (!para) {
  417.             para = 1;    /* not in para so start one */
  418.             tabl = 0;
  419.         }
  420.         fprintf(bo, "%s \n", &line2[1]);
  421.         }
  422.         fflush(bo);
  423.         break;
  424.     }
  425.     case '^':
  426.     break;            /* ignore */
  427.     default:{
  428.         if (isdigit((int)line[0])) {    /* start of section */
  429.         if (intable) {
  430.             intablebut = TRUE;
  431.             if (bt == NULL) {
  432.             fflush(bo);
  433.             bt = fopen("doc2ipf.tmp", "w+");
  434.             if (bt == NULL)
  435.                 fprintf(stderr, "cant open temp\n");
  436.             else
  437.                 bo = bt;
  438.             }
  439.         }
  440.         if (startpage)    /* use new level 0 item */
  441.             refs(0, bo, NULL, NULL, NULL);
  442.         else
  443.             refs(last_line, bo, NULL, NULL, NULL);
  444.         para = 0;    /* not in a paragraph */
  445.         tabl = 0;
  446.         last_line = line_count;
  447.         startpage = 0;
  448.         fprintf(stderr, "%d: %s\n", line_count, &line2[1]);
  449.         klist = lookup(&line2[2]);
  450.         if (klist != NULL)
  451.             k = klist->line;
  452.         /*if( k<0 ) fprintf(bo,":h%c.", line[0]=='1'?line[0]:line[0]-1);
  453.            else */
  454.         fprintf(bo, ":h%c res=%d.", line[0], line_count);
  455.         fprintf(bo, &(line2[1]));    /* title */
  456.         fprintf(bo, "\n:p.");
  457.         } else
  458.         fprintf(stderr, "unknown control code '%c' in column 1, line %d\n",
  459.             line[0], line_count);
  460.     }
  461.     break;
  462.     }
  463. }
  464.